package com.ctsi.ssdc.service.impl;


import com.ctsi.ssdc.repository.GoodsProductMapper;
import org.springframework.stereotype.Service;
import org.apache.commons.collections.CollectionUtils;
import com.ctsi.ssdc.domain.LitemallGoodsProduct;
import com.ctsi.ssdc.domain.LitemallGoodsProductExample;
import com.ctsi.ssdc.service.LitemallGoodsProductService;
import com.ctsi.ssdc.repository.LitemallGoodsProductRepository;
import org.apache.commons.lang3.StringUtils;
import java.util.List;
import java.util.Objects;
import com.ctsi.ssdc.service.impl.StrengthenBaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import com.ctsi.ssdc.model.PageResult;
import org.springframework.data.domain.Pageable;
import com.github.pagehelper.PageHelper;
/**
 * Service Implementation for managing LitemallGoodsProduct.
 *
 * @author ctsi-biyi-generator
 *
 */
@Service
public class LitemallGoodsProductServiceImpl 
	extends StrengthenBaseServiceImpl<LitemallGoodsProductRepository, LitemallGoodsProduct, Integer, LitemallGoodsProductExample> 
	implements LitemallGoodsProductService {

    @Autowired
    LitemallGoodsProductRepository litemallGoodsProductRepository;
    @Autowired
    GoodsProductMapper goodsProductMapper;


    /**
    * GET  /litemallGoodsProducts : get the litemallGoodsProducts firstStringBaseColumn.
    */
    @Override
    public PageResult<LitemallGoodsProduct> findFirstStringColumn(String specifications,Pageable pageable){
        String str = specifications;
        if (Objects.nonNull(pageable)) {
            PageHelper.startPage(pageable.getPageNumber() + 1, pageable.getPageSize());
        }
        LitemallGoodsProductExample litemallGoodsProductExample = new LitemallGoodsProductExample();
        String orderBy = getPageOrderBy(pageable);
        if(StringUtils.isNotEmpty(orderBy)) {
            litemallGoodsProductExample.setOrderByClause(orderBy);
        }
        if (StringUtils.isEmpty(str)){
            litemallGoodsProductExample.createCriteria();
        } else{
            litemallGoodsProductExample.createCriteria().andSpecificationsLike("%" + str +"%");
        }
        List<LitemallGoodsProduct>  data = litemallGoodsProductRepository.selectByExample(litemallGoodsProductExample);

        long count = 0L;
        if (CollectionUtils.isNotEmpty(data)){
            count = litemallGoodsProductRepository.countByExample(litemallGoodsProductExample);
        }
        return new PageResult<LitemallGoodsProduct>(data,count,count);
    }

    @Override
    public LitemallGoodsProduct findById(Integer productId) {
        return litemallGoodsProductRepository.selectByPrimaryKey(productId);
    }

    @Override
    public int reduceStock(Integer productId, Short number) {
        return goodsProductMapper.reduceStock(productId, number);
    }

    @Override
    public int addStock(Integer productId, Short number) {
        return goodsProductMapper.addStock(productId, number);
    }

    @Override
    public List<LitemallGoodsProduct> queryByGid(Integer gid) {
        LitemallGoodsProductExample example = new LitemallGoodsProductExample();
        example.or().andGoodsIdEqualTo(gid).andDeletedEqualTo(false);
        return litemallGoodsProductRepository.selectByExample(example);
    }

    private String getPageOrderBy(Pageable page) {
        if(page!= null && page.getSort() != null) {
            StringBuilder sb = new StringBuilder();
            page.getSort().forEach(sort -> sb.append(sort.getProperty())
            .append(" ").append(sort.getDirection()).append(","));
            if(sb.length() > 1) {
                return (sb.substring(0,sb.length()-1));
             }
        }
        return null;
    }
}
